package com.house365.core.application;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.house365.core.http.BaseHttpAPI;
import com.house365.core.util.lbs.OnLocationChangeListener;

/**
 * @author py
 * @date 2012-9-17
 * @comment
 */
public abstract class BaseApplicationWithMapService<T extends BaseHttpAPI>
		extends BaseApplication<T> {

	protected LocationClient bLocationClient;
	private OnLocationChangeListener locationChangerListener;

	public void onCreate() {
		super.onCreate();
		getbLocationClient()
				.registerLocationListener(new MyLocationListenner());
	}

	public LocationClient getbLocationClient() {
		if (bLocationClient == null) {
			bLocationClient = new LocationClient(this);
		}
		return bLocationClient;
	}

	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null) {
				return;
			}
			/**
			 * 61 ： GPS定位结果 62 ： 扫描整合定位依据失败。此时定位结果无效。 63 ：
			 * 网络异常，没有成功向服务器发起请求。此时定位结果无效。 65 ： 定位缓存的结果。 66 ：
			 * 离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果 67 ：
			 * 离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果 68 ：
			 * 网络连接失败时，查找本地离线定位时对应的返回结果 161： 表示网络定位结果 162~167： 服务端定位失败。
			 */
			if (location.getLocType() == 61 || location.getLocType() == 65
					|| location.getLocType() == 68
					|| location.getLocType() == 161) {
				if (locationChangerListener != null) {
					locationChangerListener.onGetLocation(location);
				}
			}

		}

		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null) {
				return;
			}
			if (poiLocation.getLocType() == 61
					|| poiLocation.getLocType() == 65
					|| poiLocation.getLocType() == 68
					|| poiLocation.getLocType() == 161) {
				if (locationChangerListener != null) {
					locationChangerListener.onGetPoi(poiLocation);
				}
			}
		}
	}

	public void removeLocationChangerListener() {
		locationChangerListener = null;
	}

	public void setLocationChangerListener(
			OnLocationChangeListener locationChangerListener) {
		this.locationChangerListener = locationChangerListener;
	}

}
